home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5534 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.3 KB

  1. Path: newshost.lanl.gov!tanmoy
  2. From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Newbie: How to return Multi-Dimensional Array's?
  5. Date: 08 Feb 1996 15:29:07 GMT
  6. Organization: Los Alamos National Laboratory
  7. Message-ID: <TANMOY.96Feb8082907@qcd.lanl.gov>
  8. References: <4f7t4m$4bk@news.xs4all.nl> <3118B3F7.2781E494@nada.kth.se>
  9. NNTP-Posting-Host: qcd.lanl.gov
  10. Mime-Version: 1.0
  11. Content-Type: text
  12. In-reply-to: Fredrik Rubensson's message of Wed, 07 Feb 1996 15:15:19 +0100
  13.  
  14. In article <3118B3F7.2781E494@nada.kth.se> Fredrik Rubensson
  15. <prgp-fru@nada.kth.se> writes: 
  16. <snip>
  17.    > char *foo()
  18.    > {
  19.    >  char arr[10][5];
  20.    > // do something
  21.    > return (arr)
  22.    > }
  23.  
  24.    A multidimensional char-array is represented as an array of
  25.    char-pointers so the return type of foo should be char**. The
  26.    parenthesis around arr after return are not needed.
  27.  
  28. Please read the FAQ: the post uncovers a very serious
  29. misunderstanding. A multidimensional array, like the one declared
  30. above is _not_ an array of char pointers. In other words, char
  31. arr[10][5] is different from char *arr[10] or [5]. It is true that the
  32. multidimensional array does not decay into a pinter to char, but
  33. neither does it decay into char**.
  34.  
  35. In short: char arr[10][5] means that arr is the name of a region of
  36. memory which consists of 10 contiguos regions, each region consisting
  37. of 5 contiguous regions capable of storing chars. sizeof(arr) is thus
  38. guaranteed to be 50. It contains no storage for any pointers. When
  39. used in any context other than being an immediate operand of & and
  40. sizeof, it decays into a pointer to the first region: which is a
  41. pointer to an array of 5 chars, i.e. char(*)[5]. Note that again, the
  42. region that this points to stores no pointer!
  43.  
  44. char ** is a pointer to a region which stores a pointer to a char. Is
  45. it obvious that the two are different?
  46.  
  47. Cheers
  48. Tanmoy
  49. --
  50. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  51. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  52. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  53. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  54. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  55. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  56.